<?php

namespace App\Http\Controllers\Mini;

use App\Models\Areas;
use App\Models\BalanceLogs;
use App\Models\Categorys;
use App\Models\Configs;
use App\Models\Demands;
use App\Models\Footprints;
use App\Models\IntegralLogs;
use App\Models\Mails;
use App\Models\ProfitLogs;
use App\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\rewardOrder;
use App\Models\OrderDetail;
use App\Http\Requests\Demand;

class DemandController extends Controller
{
    /**
     * 发布需求
     * @route api.demand.demandAdd
     * @param user_id 用户id
     * @param cate_pid 一级分类id
     * @param cate_id 二级分类id
     * @param title 需求标题
     * @param content 需求描述1
     * @param price 预算（单位：万）
     * @param area 需求区域
     * @param end_at 截止时间
     */
    public function demandAdd(Request $request){
        try{
            $user_id = (int)$request->post('user_id');        
            if (empty($user_id)) return response()->json(["code"=>0, "msg"=>'无效用户']);                       
            $users = Users::findOrFail($user_id);
            if (empty($users)) return response()->json(["code"=>0, "msg"=>'用户不存在']);
            if($users["type"] != 3){
             	 return response()->json(["code"=>0, "msg"=>'无权发布']);
            }
            
            if($users["status"] == 4 ){
            	return response()->json(["code"=>0, "msg"=>'您已被拉入黑名单，无法发布任务']);
            }
            
            $reward_id = (int)$request->post('reward_id');
              
            $params = [
                'user_id' => $user_id,
                'cate_pid' => (int)$request->post('cate_pid'),
                'cate_id' => (int)$request->post('cate_id'),
                'title' => (string)$request->post('title'),
                'area' => (int)$request->post('area'),
            	'pttype_id' => (int)$request->post('pttype_id'),
            	'start_at' => $request->post('start_at'),
                'end_at' => (string)$request->post('end_at'),
                'price' => (string)$request->post('price'),
                'content' => (string)$request->post('content'),
            	//'unit' =>	$request->post('unit'),
            	'mission_limit'	=>$request->post('mission_limit'),
            	'status'=>1,
            	'number'=>	$request->post('number'),
                'step1_desc'=> $request->post('step1_desc'),   
            	'step1_image'=> $request->post('step1_image'),
            	'step2_desc'=> $request->post('step2_desc'),
            	'step2_image'=> $request->post('step2_image'),
            	'step3_desc'=> $request->post('step3_desc'),
            	'step3_image'=> $request->post('step3_image'),
            	'step4_desc'=> $request->post('step4_desc'),
            	'step4_image'=> $request->post('step4_image'),
            	'step5_desc'=> $request->post('step5_desc'),
            	'step5_image'=> $request->post('step5_image'),
            	'step6_desc'=> $request->post('step6_desc'),
            	'step6_image'=> $request->post('step6_image'),
            ];
            
            if($users['balance']<$params['number']*$params['price']){
            	return response()->json(["code"=>0, "msg"=>'余额不足，请及时充值']);
            }
            
            if($reward_id>0){
            	$params['status'] = 3;
            	$info = Demands::findOrFail($reward_id);
            	$info->update($params);
            	$msg = "修改成功！";
            }else{
            	$lists = Demands::create($params);            	
            	Users::where('id',$user_id)->increment('publish_num');
            	$msg =  "发布成功,请等待平台审核!";
            }
           
            
            return response()->json([
                "code" => 1,
                "msg" => $msg,
                "data" => ''
            ]);
           
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }
    
	/**
	 * B端强行终止任务
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function stopReward(Request $request){
    	try{
	    	$user_id = (int)$request->post('user_id');
	    	if (empty($user_id)) return response()->json(["code"=>0, "msg"=>'无效用户']);
	    	$users = Users::findOrFail($user_id);
	    	if (empty($users)) return response()->json(["code"=>0, "msg"=>'用户不存在']);
// 	    	if($users["type"] != 3){
// 	    		return response()->json(["code"=>0, "msg"=>'无权修改']);
// 	    	}
	    	
	    	$reward_id = (int)$request->post('reward_id');
	    	$info = Demands::findOrFail($reward_id);
	    	if(empty($info)){
	    		return response()->json(["code"=>0, "msg"=>'任务数据不存在']);
	    	}else{
	    		$data['status'] = 4;
	    		$info->update($data);
	    	}
	    	
	    	return response()->json([
    			"code" => 1,
    			"msg" => "已停止",
    			"data" => ''
	    	]);
    	}catch (\Exception $e){
    		return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
    	}
    }
    
    /**
     * 需求列表
     * @route api.demand.demandLists
     * @param cid 一级分类id
     * @param cid_id 二级分类id
     * @param area 区域id
     * @param page 页码
     * @param sort 排序字段   created_at 发布时间   price 赏金价格
     * @param pttype 筛选类型  int类型，悬赏类型ID
     * @param sort_by 'desc'倒叙  ‘asc'正序
     * @return bool
     * @return （array）children 二级分类
     * @return （array）demand 活动列表
     * @return （array）area 区域
     */
    public function demandLists(Request $request){
        try{
        	
            $page = (int)$request->post('page',1);//页码
            $limit = (int)$request->post('limit',10);//条数
            $params = [
                'cid' => (int)$request->post('cid'),//一级分类id
                'cid_id'=>(int)$request->post('cid_id'),//二级分类
//                 'area' => (int)$request->post('area'),//区域id   
                'keywords'=> $request->post('keywords'),
            	'pttype_id'=>(int)$request->post('pttype_id'),
            	'show'=>(int)$request->post('show')
            ];
            $params['status'] = 3;
         //   $params['end_at'] = ['>=',date("Y-m-d H:i:s",time())];
            $sort = $request->post('sort','view');   //排序字段
            
            $sort_by =  $request->post('sort_by','desc'); //发布时间排序  asc 升序，desc 降序
            
          //  var_dump($params);
            $fields = ['id','user_id','title','price','ownnumber','number','start_at','end_at','created_at','content','status','view','area','unit','cate_id','pttype_id','cate_pid'];
            $lists = Demands::allLists($params, $fields, $page, $limit, $sort, $sort_by);
            $base = Configs::getConfig('base');
            $user_id = (int)$request->post('user_id',0);
            
            foreach ($lists as $key=>$val){
                $lists[$key]['start_at'] = trim(@date('m-d', strtotime($val['created_at'])));
                $lists[$key]['end_at'] = trim(@date('m-d', strtotime($val['end_at'])));
                $lists[$key]['price'] = floatval($val['price']*$base['price_rate']/100);  //任务报酬要扣除平台费用
              
                $order = rewardOrder::where(array('user_id'=>$user_id,'reward_id'=>$val['id']))
                ->orderBy("created_at",'desc')
                ->first(['id','user_id','reward_id','end_time','submit_times','status','sumit_at']);
             
                if($order['id']>0 && $user_id>0){
                	if($order['status'] == 1){
                
                		 $lists[$key]['user_status'] = "进行中";
                		 $lists[$key]['user_end_time'] =strtotime($order['end_time']);
                	}
                	if($order['status'] == 2){
                		 $lists[$key]['user_status'] = "等待审核";
                		//自动审核时间
                		 $lists[$key]['user_end_time'] =strtotime($order['sumit_at']+12*3600);
                	}
                	if($order['status'] == 3){
                		 $lists[$key]['user_status'] = "已完成";
                	}
                	if($order['status'] == 4){
                		 $lists[$key]['user_status'] = "任务失败";
                	} 
                	if($order['status'] == 5){
                		$lists[$key]['user_status'] = "已超时";
                	}             	
                }
            }

            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    /**
     * 任务报名
     * @return \Illuminate\Http\JsonResponse
     */
    public function signUpDemand(Request $request){
    	try {
    		$user_id = (int)$request->post('user_id');
            if (empty($user_id)) {
            	return response()->json(["code"=>0, "msg"=>'数据异常']);
            }
            
            $users = Users::where('id',$user_id)->where('status','<>',-1)->first(['true_name','type']);
             
            if (empty($users)){
            	return response()->json(["code"=>0, "msg"=>'登录异常']);
            }
            
            if($users["status"] ==4 ){
            	return response()->json(["code"=>0, "msg"=>'您已被拉入黑名单，无法报名']);
            }
            
            if($users["type"] ==3 ){
            	return response()->json(["code"=>0, "msg"=>'悬赏主不能接受任务']);
            }
            //查看是否已经报过名           
            $reward_id = (int)$request->post('reward_id');
            if (empty($reward_id)) {
            	return response()->json(["code"=>0, "msg"=>'任务数据丢失']);
            }
            $reward = Demands::where('id',$reward_id)->first();
           
            if(empty($reward)){
            	return response()->json(["code"=>0, "msg"=>'任务数据异常']);
            }
            
            if(strtotime($reward['start_at']) > time()){
            	return response()->json(["code"=>0, "msg"=>'任务未开始']);
            }
         
            if(strtotime($reward['end_at']) < time()){
            	return response()->json(["code"=>0, "msg"=>'任务已过期']);
            }
            
            if($reward['ownnumber']>=$reward['number']){
            	return response()->json(["code"=>0, "msg"=>'任务已完成']);
            }
            if($reward['status'] == 2) {
            	return response()->json(["code"=>0, "msg"=>'任务下架']);
            }
            if($reward['status'] == 4) {
            	return response()->json(["code"=>0, "msg"=>'任务已结束']);
            }
                  
            $order = rewardOrder::where(array('user_id'=>$user_id,'reward_id'=>$request->post('reward_id'),['status','<=',4]))->first("id");
            if($order['id']>0){
            	return response()->json(["code"=>0, "msg"=>'您已经报过名了']);
            }
            
            $base = Configs::getConfig('base');
        
            $getMoney = sprintf("%.2f",$reward['price']*$base['price_rate']/100);
            $params = [
            		'user_id' => $user_id,
            		'reward_id' => (int)$request->post('reward_id'),
            		'get_money' => $getMoney,
            		'status'=>1,
            		'check_status'=>0,
            		'submit_times'=>0         		
            ];
  			if($reward['mission_limit']>0){
  				$endtime = date('Y-m-d H:i:s',time()+$reward['mission_limit']*3600);
  				$params['end_time']= $endtime;
  			}
            
            $order_id = rewardOrder::create($params);
            if (!$order_id) {           	
            	return response()->json(["code"=>0, "msg"=>'报名失败[未生成报名数据]']);
            }
            $detail = [
            		'user_id'=>$user_id,
            		'reward_id' => (int)$request->post('reward_id'),
            		'order_id'=>$order_id['id']
            ];
            $detail_id = OrderDetail::create($detail);
            if (!$detail_id) {				            	
            	return response()->json(["code"=>0, "msg"=>'报名失败[详情生成失败]']);
            }
            
        //    Demands::where('id',$request->post('reward_id'))->increment('ownnumber');
            return response()->json([
            		"code" => 1,
            		"msg" => "报名成功"
            ]);
           
    	}catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }
    
    /**
     * 任务详情B端
     * @param Request $request
     */
    public function getDemandInfoForUser(Request $request){
    	try{
    		$user_id = (int)$request->post('user_id');
    		$reward_id = (int)$request->post('reward_id');
    		if (empty($reward_id)) {
    			return response()->json(["code"=>0, "msg"=>'数据异常']);
    		}
    		$where['id'] = $reward_id;
   
    		$reward = Demands::details($where,'*');
    		
    		if($reward['id'] != $reward_id){
    			return response()->json(["code"=>0, "msg"=>'非法路径']);
    		}
    		
    		$reward['rest_num'] = $reward['number'] - $reward['ownnumber'];
    		$params=[
    			'reward_id'=>$reward_id,
    			'status'=>2,
    			'check_status'=>0
    		];
    		
    		$orders = rewardOrder::where($params)->get(); 
    		
    		foreach ($orders  as $key=>$val){
    			$uinfo = Users::where('id',$val['user_id'])->first(['id','true_name','nickname']);
    			$orders[$key]['order_user_name'] = $uinfo['true_name']?$uinfo['true_name']:$uinfo['nickname'];
    			
    		} 	
    		if(!empty($reward)){
    			$reward['orders'] = $orders;
    		}
    		return response()->json([
    				"code" => 1,
    				"msg" => "获取成功",
    				"data" => $reward
    		]);
    	}catch (\Exception $e){
    		return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
    	}
    }
    /**
     * 获取任务详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDemandInfo(Request $request){
    	try{
    		$user_id = (int)$request->post('user_id');
    		$reward_id = (int)$request->post('reward_id');
    		if (empty($reward_id)) {
    			return response()->json(["code"=>0, "msg"=>'数据异常']);
    		}
    		$where['id'] = $reward_id;
    		$reward = Demands::details($where,'*');
    		$base = Configs::getConfig('base');
    		$reward['price'] = floatval($reward['price']*$base['price_rate']/100);  //任务报酬要扣除平台费用
    		$reward['rest_num'] = $reward['number'] - $reward['ownnumber'];
    		$order = rewardOrder::where(array('user_id'=>$user_id,'reward_id'=>$reward_id,['status','<=',4]))
    			    ->orderBy("created_at",'desc')
    				->first(['id','user_id','reward_id','end_time','submit_times','status','sumit_at','staus_end_time']);
    		$reward['flag'] = 0;
    		$reward['u_status'] = 0; //没报名，可以点击报名  1 
    		$reward['staus_end_time'] = $order['staus_end_time'];
    		if($reward['user_id'] == $user_id){
    			$reward['u_status'] = 6; //任务属于自己，可以进行编辑操作
    		}
    		
    		if($order['id']>0){
    			$reward['flag'] = 0;
    			$reward['order_id'] = $order['id'];   			
    			$reward['u_status'] = $order['status'];
    			if($order['status'] == 1){
    				
    				$reward['user_status'] = "进行中";
    				$reward['user_end_time'] =strtotime($order['end_time']);
    			}
    			if($order['status'] == 2){
    				$reward['user_status'] = "等待审核";
    				//自动审核时间
    				$reward['user_end_time'] =strtotime($order['sumit_at']+12*3600);
    			}
    			if($order['status'] == 3){
    				$reward['user_status'] = "已完成";
    			}
    			if($order['status'] == 4){
    				$reward['user_status'] = "任务失败";
    				if($order['submit_times']<2){
    					$reward['flag'] = 1;
    				}    				
    			}
    			if($order['status'] == 5){
    				$reward['user_status'] = "任务超时";
    				if($order['submit_times']<2){
    					$reward['flag'] = 1;
    				}
    			}
				//获取任务审核信息
				$remark_info = OrderDetail::where('order_id',$order['id'])->first();
				$reward['order_detail'] = $remark_info;
				$base = Configs::getConfig('base');
				$reward['price'] = $reward["price"]*$base['price_rate']/100;
    		
    		}
    		return response()->json([
    				"code" => 1,
    				"msg" => "获取成功",
    				"data" => $reward
    		]);
    	}catch (\Exception $e){
    		return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
    	}
    }
    
    
    /**
     * 任务提交
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function signsubmitDemand(Request $request){
    	try {
    		$user_id = (int)$request->post('user_id');
    		if (empty($user_id)) {
    			return response()->json(["code"=>0, "msg"=>'数据异常']);
    		}
    		
    		$order_id = (int)$request->post('order_id');
    		$order = rewardOrder::where("id",$order_id)->first(['id','user_id','reward_id','end_time','submit_times','check_status','staus_end_time']);
    		if(!$order){
    			return response()->json(["code"=>0, "msg"=>'报名数据异常']);
    		}
    		
    		if($order['user_id'] != $user_id){
    			return response()->json(["code"=>0, "msg"=>'任务匹配失败']);
    		}
    		if($order['submit_times'] >=2){
    			return response()->json(["code"=>0, "msg"=>'只能提交2次,你可以提起申述']);
    		}   		
    		if($order['check_status'] == 0 && $order['status'] == 2){
    			return response()->json(["code"=>0, "msg"=>'审核中，请等待']);
    		}
    		
    		if($order['status'] == 3){
    			return response()->json(["code"=>0, "msg"=>'任务已完成']);
    		}
    		if($order['status'] == 4){
    			return response()->json(["code"=>0, "msg"=>'任务已失败，可以重新申请或者提起申述']);
    		}
    		
    		if($order['status'] == 5){
    			return response()->json(["code"=>0, "msg"=>'任务超时']);
    		}
    		
    		$reward = Demands::where('id',$order['reward_id'])->first();    
    		
    		if(strtotime($order['end_time'])<time() && $order['submit_times'] ==0){
    			$order = [
    				'status'=>5,
    				'check_status'=>2			
    			];
    			$info = rewardOrder::findOrFail($order_id);
    			$info->update($order);
    			
    			$template = [
    					'title' => '任务失败',
    					'content' => '您接受的任务"' . $reward['title'] .'"超时，视为失败，你可以重新接受该任务',
    					'user_id' => $user_id,
    					'status' => 1,
    					'type' => 1,
    			];
    			Mails::create($template);   			
    			return response()->json(["code"=>0, "msg"=>'任务超时，请重新报名']);
    		}
    		
    		$step1_image = (string)$request->post('step1_image','');   		
    		$step2_image = (string)$request->post('step2_image','');
    		$step3_image = (string)$request->post('step3_image','');
    		$step4_image = (string)$request->post('step4_image','');
    		$step5_image = (string)$request->post('step5_image','');   		
    		$step6_image = (string)$request->post('step6_image','');
    		
    		$step1_user_desc = (string)$request->post('step1_user_desc','');
    		$step2_user_desc = (string)$request->post('step2_user_desc','');
    		$step3_user_desc = (string)$request->post('step3_user_desc','');
    		$step4_user_desc = (string)$request->post('step4_user_desc','');
    		$step5_user_desc = (string)$request->post('step5_user_desc','');
    		$step6_user_desc = (string)$request->post('step6_user_desc','');
    		
    		//需要做的事情
    		//检查该任务是否有步骤说明
    		//有的话就要上传对应步骤的图
    		if($order['submit_times'] < 2){
    			
    			if($order['staus_end_time'] < time() && $order['submit_times'] == 1){
    				$order = [
    						'status'=>4,
    						'check_status'=>2
    				];
    				$info = rewardOrder::findOrFail($order_id);
    				$info->update($order);
    				 
    				$template = [
    						'title' => '任务失败',
    						'content' => '您接受的任务"' . $reward['title'] .'"时限已过，不能再提交审核，视为失败',
    						'user_id' => $user_id,
    						'status' => 1,
    						'type' => 1,
    				];
    				Mails::create($template);
    				return response()->json(["code"=>0, "msg"=>'时限已过，不能再提交审核，视为失败']);
    			}
    		
//     			if(isset($reward['step1_image'])&&!empty($reward['step1_image'])){
//     				if($step1_image == ""){
//     					return response()->json(["code"=>0, "msg"=>'请上传第一步完成图']);
//     				}
//     			}
//     			if(isset($reward['step2_image'])&&!empty($reward['step2_image'])){
//     				if($step2_image == ""){
//     					return response()->json(["code"=>0, "msg"=>'请上传第二步完成图']);
//     				}
//     			}
//     			if(isset($reward['step3_image'])&&!empty($reward['step3_image'])){
//     				if($step3_image == ""){
//     					return response()->json(["code"=>0, "msg"=>'请上传第三步完成图']);
//     				}
//     			}
//     			if(isset($reward['step4_image'])&&!empty($reward['step4_image'])){
//     				if($step4_image == ""){
//     					return response()->json(["code"=>0, "msg"=>'请上传第四步完成图']);
//     				}
//     			}
//     			if(isset($reward['step5_image'])&&!empty($reward['step5_image'])){
//     				if($step5_image == ""){
//     					return response()->json(["code"=>0, "msg"=>'请上传第五步完成图']);
//     				}
//     			}
//     			if(isset($reward['step6_image'])&&!empty($reward['step6_image'])){
//     				if($step6_image == ""){
//     					return response()->json(["code"=>0, "msg"=>'请上传第六步完成图']);
//     				}
//     			}
    			
    			$order = [
    					'status'=>2,
    					'check_status'=>0,
    					'staus_end_time'=>time()+3600*12
    			];
    			$info = rewardOrder::findOrFail($order_id);
    			$info->update($order);

    			$orderdetail = [
    					'step1_image' =>$step1_image,
    					'step2_image' =>$step2_image,
    					'step3_image' =>$step3_image,
    					'step4_image' =>$step4_image,
    					'step5_image' =>$step5_image,
    					'step6_image' =>$step6_image,
    					'step1_user_desc' =>$step1_user_desc,
    					'step2_user_desc' =>$step2_user_desc,
    					'step3_user_desc' =>$step3_user_desc,
    					'step4_user_desc' =>$step4_user_desc,
    					'step5_user_desc' =>$step5_user_desc,
    					'step6_user_desc' =>$step6_user_desc
    			];   			
    			OrderDetail::where('order_id',$order_id)->update($orderdetail);
    			rewardOrder::where('id',$order_id)->increment('submit_times');  
    			$info = rewardOrder::findOrFail($order_id);
    			return response()->json([
    					"code" => 1,
    					"msg" => "已提交，请等待悬赏主审核",
    					"end_time"=>(strtotime($info['updated_at'])+3600*12)
    			]);
    		} else{
    			return response()->json(["code"=>0, "msg"=>'次数限制']);
    		}    		
    	}catch (\Exception $e){
    		return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
    	}	
    }

    /**
     * 获取提交审核的任务
     * @return \Illuminate\Http\JsonResponse
     */
    public function getOrderdetailInfo(Request $request){
    	try {
    		$order_id = (int)$request->post('order_id');
    		$order = rewardOrder::where("id",$order_id)->first(['id','user_id','reward_id','end_time','submit_times','check_status','get_money']);
    		//var_dump($order);
    		if(!$order) {
    			return response()->json(["code"=>0, "msg"=>'数据异常']);
    		}
    		$remark_info = OrderDetail::where('order_id',$order['id'])->first(['*']);
    		$order['order_detail'] = $remark_info;
    		$reward_info = Demands::where('id',$order['reward_id'])
    			->first(['step1_image','step2_image','step3_image','step4_image','step5_image','step6_image','step1_desc','step2_desc','step3_desc','step4_desc','step5_desc','step6_desc']);//findOrFail($order['reward_id']);
    		$order['reward'] = $reward_info;
    		return response()->json([
    				"code" => 1,
    				"msg" => "获取成功",
    				"order"=>$order
    		]);
    		
    	}catch (\Exception $e){
    		return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
    	}
    }
    
    /**
     * 悬赏主审核
     * 
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function checkRewardStatus(Request $request){
    	try {
    		$user_id = (int)$request->post('user_id');
    		if (empty($user_id)) {
    			return response()->json(["code"=>0, "msg"=>'数据异常']);
    		}
    		$order_id = (int)$request->post('order_id');
    		$order = rewardOrder::where("id",$order_id)->first(['id','user_id','reward_id','end_time','submit_times','check_status','get_money']);   		
    		if(!$order) {
    			return response()->json(["code"=>0, "msg"=>'数据异常']);
    		}
    		    		 		
    		if($order['check_status'] >0 ){   			
    			return response()->json(["code"=>0, "msg"=>'已经审核过了']);
    		}
     		 		
    		$reward = Demands::where('id',$order['reward_id'])->first();
    		if($reward['user_id'] != $user_id){
    			return response()->json(["code"=>0, "msg"=>'无审核权限']);
    		}
    		$check_status = (int)$request->post('status');
    		if(!$check_status){
    			return response()->json(["code"=>0, "msg"=>'请选择审核是否通过']);
    		}
    		
    		$order['check_status']=$check_status;
    		
    		//审核通过
    		if($check_status == 1){
    			//审核时，已完成数达到任务总数，直接视为失败
    			if($reward['ownnumber']>=$reward['number']){    						
    				$order1['status'] = 4;
    				$order1['check_status'] = 2;
    				$info = rewardOrder::findOrFail($order_id);
    				$info->update($order1);
    				$content = '您接受的任务"' .$reward['title'] .'"审核失败，其它赏金猎人先于您一步完成，请下次加油';
    			}else{
    				
    				$order2['status'] = 3;
    				$order2['check_status'] = 1;
    				$info = rewardOrder::findOrFail($order_id);
    				$info->update($order2);
    				//新增余额
    				Users::where('id',$order['user_id'])->increment('balance',$order['get_money']);
    				//悬赏这扣除余额
    				Users::where('id',$reward['user_id'])->decrement('balance',$reward['price']);
    				$balance = [
    						'user_id' => $reward['user_id'],
    						'user_name' => Users::where('id',$reward['user_id'])->value('nickname'),
    						'money' => $reward['price'],
    						'balance' => '-'.$reward['price'],
    							
    						'desc' => '支付赏金',
    						'type' => 1,
    				];
    				$balance_info = BalanceLogs::create($balance);
    				
    				//成功数+1
    				Demands::where('id',$order['reward_id'])->increment('ownnumber');
    				$content = '您接受的任务"' .$reward['title'] .'"审核通过，赏金增加'.$order['get_money'];

    				$user_info = Users::where('id',$order['user_id'])->where('status','<>',-1)->first(['true_name','nickname','inviter_id']);
    				$balance = [
    						'user_id' => $order['user_id'],
    						'user_name' => $user_info['true_name']?$user_info['true_name']:$user_info['nickname'],
    						'money' => $order['get_money'],
    						'balance' => '+'.$order['get_money'],
    						 
    						'desc' => '任务收益',
    						'type' => 1,
    				];
    				$balance_info = BalanceLogs::create($balance);   

    				//查询是否有团长，有的话团长金额增加
    				if($user_info['inviter_id']>0){
    					//有上级，查看是否有返佣资格
    					$my_numbers = Users::where(array("inviter_id"=>$user_info['inviter_id'],"type"=>2,"status"=>2))->count("id");
    					if($my_numbers>=10){
    						$base = Configs::getConfig('base');
    						
    						Users::where('id',$user_info['inviter_id'])->increment('balance',$order['get_money']*$base['captain_rate']/100);
    						$msg = '您的团员'.$user_info['nickname'].'完成任务"' .$reward['title'] .'"获得佣金'.$order['get_money']*$base['captain_rate']/100;
    						$template1 = [
    								'title' => '佣金提成通告',
    								'content' => $msg,
    								'user_id' => $user_info['inviter_id'],
    								'status' => 1,
    								'type' => 1
    						];
    						//站内信
    						Mails::create($template1);
    						$inviter = Users::where('id',$user_info['inviter_id'])->where('status','<>',-1)->first(['true_name','nickname','inviter_id']);
    						$balance = [
    								'user_id' => $user_info['inviter_id'],
    								'user_name' => $inviter['true_name']?$inviter['true_name']:$inviter['nickname'],
    								'money' => $order['get_money']*$base['captain_rate']/100,
    								'balance' => '+'.$order['get_money']*$base['captain_rate']/100,   									
    								'desc' => '团员收益提成',
    								'type' => 1,
    						];
    						$balance_info = BalanceLogs::create($balance);
    					}
    				} 
    			}	  			
    		}
    		
    		//审核不通过
    		if($check_status == 2){
    			$order3['status'] = 4;
    			$order3['check_status'] = 2;
    			$order3['staus_end_time']=time()+3600*12;
    			$info = rewardOrder::findOrFail($order_id);
    			$info->update($order3);
    			$content = '您接受的任务"' .$reward['title'] .'"审核不通过，请到任务中心查看详情';
    		}
    		
    		$template = [
    				'title' => '任务通告',
    				'content' => $content,
    				'user_id' => $order['user_id'],
    				'status' => 1,
    				'type' => 1
    		];
    		//站内信
    		Mails::create($template);
    		
    		//每个步骤的审核信息
    		$step1_remark = (string)$request->post('step1_remark','');
    		$step2_remark = (string)$request->post('step2_remark','');
    		$step3_remark = (string)$request->post('step3_remark','');
    		$step4_remark = (string)$request->post('step4_remark','');
    		$step5_remark = (string)$request->post('step5_remark','');
    		$step6_remark = (string)$request->post('step6_remark','');
    		
    		$orderdetail = [
    				'step1_remark' =>$step1_remark,
    				'step2_remark' =>$step2_remark,
    				'step3_remark' =>$step3_remark,
    				'step4_remark' =>$step4_remark,
    				'step5_remark' =>$step5_remark,
    				'step6_remark' =>$step6_remark
    		];
    		OrderDetail::where('order_id',$order_id)->update($orderdetail);
    		
    		return response()->json([
    				"code" => 1,
    				"msg" => "操作成功"
    		]);
    		
    	}catch (\Exception $e){
    		return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
    	}	
    }
    
}